
#include "MapEditView.h"
#include "Application.h"
#include "ButtonItem.h"
#include "hgegui.h"
#include "TitleMenuItem.h"
#include "IntegerItem.h"
#include "Unit.h"
#include "hgeUtil.h"

//
// Constructor
//

MapEditView::MapEditView()
{

	// Loads in the tile sets
	GetTileSet("data/Tiles/set1.bmp");

	// Initialize index variables
	currentTileSet = 0;
	tile = 100;
	arrow = -1;

	// Create gui
	CreateGui();
}

//
// CreateGui
//

void MapEditView::CreateGui()
{
	editGui = new hgeGUI();

	// Buttons to switch between the tile sets
	editGui->AddCtrl(new ButtonItem(1, ScreenWidth()-140, ScreenHeight()-250, 32, 32, leftArrow));
	editGui->AddCtrl(new ButtonItem(2, ScreenWidth()-90, ScreenHeight()-250, 32, 32, rightArrow));
	
	for(int i = 0; i < int(tileSets[currentTileSet].setSize); i++)
		editGui->AddCtrl(new ButtonItem(i+100, ScreenWidth()-125, 10+i*80.0f, 64, 64, tileSets[currentTileSet].tex, int(tileSets[currentTileSet].setSize), 0xFF00FFFF));
	((ButtonItem*)editGui->GetCtrl(tile))->Select();

	hgeFont *fnt = GetSmallFont();
	editGui->AddCtrl(new TitleMenuItem(200, fnt, menuSound, 25, ScreenHeight() - 165, 0.0f, "32 x 32"));
	editGui->AddCtrl(new TitleMenuItem(201, fnt, menuSound, 25, ScreenHeight() - 150, 0.1f, "64 x 64"));
	editGui->AddCtrl(new TitleMenuItem(202, fnt, menuSound, 25, ScreenHeight() - 135, 0.2f, "128 x 128"));
	editGui->AddCtrl(new TitleMenuItem(203, fnt, menuSound, 25, ScreenHeight() - 90,  0.3f, "Save Map"));
	editGui->AddCtrl(new TitleMenuItem(204, fnt, menuSound, 25, ScreenHeight() - 75,  0.4f, "Load Map"));

	for(int i = 0; i < 7; i++)
	{
		editGui->AddCtrl(new IntegerItem(300+i, 230+i*60.0f, ScreenHeight()-190, 0, 0, 100));
		editGui->AddCtrl(new IntegerItem(307+i, 230+i*60.0f, ScreenHeight()-110, 0, 0, 100));
	}

	editGui->AddCtrl(new IntegerItem(400, 160, ScreenHeight()-120, 0, 0, 100));
}

//
// Enter
//

void MapEditView::Enter()
{
	map->LoadTerrain("data/mapSave.txt");
}

//
// ProcessGui
//

bool MapEditView::ProcessGui(const float dt)
{
	// We update the GUI and take an action if
	// one of the menu items was selected
	int id = editGui->Update(dt);
	if(id > 0)
	{
		if(id < 100)
		{
			if(arrow != -1 && arrow != id)
				GetButton(editGui, arrow)->Unselect();

			arrow = id;
		}
		else if(id < 200)
		{
			if(tile != -1 && tile != id)
				GetButton(editGui, tile)->Unselect();

			tile = id;
		}
		else if(id < 300)
		{
			switch(id)
			{
			case 200:
				map->Resize(32, 32);
				break;
			case 201:
				map->Resize(64, 64);
				break;
			case 202:
				map->Resize(128, 128);
				break;
			case 203:
				map->SaveTerrain("data/mapSave.txt");
				break;
			default:
				break;
			}
		}
		else if(id < 400)
		{
			IntegerItem *item = GetInteger(editGui, id);
			map->unitSpawn.SetWaveUnit(GetInteger(editGui, 400)->value, id-300, item->value);
		}
		else
		{
			IntegerItem *item = GetInteger(editGui, id);
			if(!item->decreasedLast && item->value != 0 && !map->unitSpawn.WaveHasUnits(item->value-1))
				item->value--;
			else
			{
				for(int i = 0; i < 14; i++)
					GetInteger(editGui, 300+i)->value = map->unitSpawn.GetWaveUnit(item->value, i);
			}
		}
	}

	return false;
}

//
// ProcessInput
//

bool MapEditView::ProcessInput(const float dt)
{
	if(MapView::ProcessInput(dt)) return true;

	// React to the mouse
	float x, y;
	hge->Input_GetMousePos(&x, &y);

	// Clicking 
	if(x < ScreenWidth()-200 && y < ScreenHeight() - 200)
		if(hge->Input_GetKeyState(HGEK_LBUTTON))
			map->SetTerrainPoint(viewArea, x, y, tile-1);

	return false;
}

//
// Render
//

void MapEditView::Render()
{
	// Render the map first then the gui over it
	RenderMap(true);
	MapView::RenderGui();
	RenderGui();

	// Render the cursor
	RenderCursorSprite();
}

//
// RenderGui
//

void MapEditView::RenderGui()
{
	// Render gui
	SetQuadRect(guiQuad, ScreenWidth()-200, 0, ScreenWidth(), ScreenHeight()-200);
	hge->Gfx_RenderQuad(&guiQuad);
	SetQuadRect(guiQuad, 0, ScreenHeight()-200, ScreenWidth(), ScreenHeight());

	editGui->Render();

	GetLargeFont()->SetColor(0xFF00FF00);
	GetLargeFont()->printf(20, ScreenHeight()-195, HGETEXT_LEFT, "Map Size");
	GetLargeFont()->printf(20, ScreenHeight()-120, HGETEXT_LEFT, "File");
	GetLargeFont()->printf(150, ScreenHeight()-150, HGETEXT_LEFT, "Wave");

	// Render the units
	for(int i = 0; i < (int)Unit::units.size()/2; i++)
	{
		Unit::units[i]->Render(230 + i*60.0f, ScreenHeight() - 160, 60);
		Unit::units[i+Unit::units.size()/2]->Render(230 + i*60.0f, ScreenHeight() - 80, 60);
	}
}

